package com.winona.ui;

import java.util.HashMap;

import org.json.JSONObject;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.R;
import com.taplinker.core.ResourceManager;
import com.taplinker.core.VersionSelecter;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.winona.Base1Activity;
import com.winona.ConstantValue;
import com.winona.domain.DataCenter;

public class SystemSetActivity extends Base1Activity {
	private View system_back;
	private TextView title;
	private Button unLogin;

	private ImageView mBt1;
	private ImageView mBt2;
	private ImageView mBt3;

	private long settingId = 0l;

	private boolean[] switchBtValue = { false, false, false };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.system_settings);

		findView();
		setLinstener();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initSetting();
	}

	private void setLinstener() {
		system_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(PersonalSettingActivity.class);
				finish();
			}
		});
		unLogin.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!VersionSelecter.hasAPI11()) {
					setResult(RESULT_OK);
					System.exit(0);
					return;
				}
				Intent intent = new Intent(SystemSetActivity.this,
						LoginActivity.class);
				intent.putExtra("relogin", true);
				// ResourceManager.getInstance().releaseAll();
				if (VersionSelecter.hasAPI11()) {
					clearTask(intent);
				}
				startActivity(intent);
				overridePendingTransition(R.anim.input, R.anim.out);

			}
		});
		mBt1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				doUpdate(!mBt1.isSelected(), mBt2.isSelected(),
						mBt3.isSelected());
				mBt1.setSelected(!mBt1.isSelected());
			}
		});
		mBt2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				doUpdate(mBt1.isSelected(), !mBt2.isSelected(),
						mBt3.isSelected());
				mBt2.setSelected(!mBt2.isSelected());
			}
		});
		mBt3.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				doUpdate(mBt1.isSelected(), mBt2.isSelected(),
						!mBt3.isSelected());
				mBt3.setSelected(!mBt3.isSelected());
			}
		});
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void clearTask(Intent it) {
		if (VersionSelecter.hasAPI11()) {
			it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
		}
	}

	private void doUpdate(final boolean bool1, final boolean bool2,
			final boolean bool3) {
		// if (!SomeUtils.islogined(this)) {
		// reset();
		// return;
		// }
		new SimpleBaseCommand() {

			@Override
			public Result go() {
				HashMap<String, String> params = new HashMap<String, String>();
				params.put("id", "" + settingId);
				params.put("userId", ""
						+ AppConfigFactory.getInstance().getUserInfo()
								.getUserid());
				// params.put("sdkAccount", "user_"
				// + AppConfigFactory.getInstance().getUserInfo()
				// .getUserName());
				params.put("patientsRemind", bool1 ? "1" : "0");
				params.put("doctorRemind", bool2 ? "1" : "0");
				params.put("orderactivityRemind", bool3 ? "1" : "0");
				return new HttpTemplate().postForForm(ConstantValue.COMMON
						+ "/expert/setting/update", params);
			}

			@Override
			public void yes(Result result) {
				// Toast.makeText(getApplicationContext(), "设置成功",
				// Toast.LENGTH_SHORT).show();
				switchBtValue[0] = bool1;
				switchBtValue[1] = bool2;
				switchBtValue[2] = bool3;
			}

			@Override
			public void no(Result result) {
				// Toast.makeText(getApplicationContext(), "设置失败",
				// Toast.LENGTH_SHORT).show();
				reset();
			}

		}.execute();
	}

	private void reset() {
		mBt1.setSelected(switchBtValue[0]);
		mBt2.setSelected(switchBtValue[1]);
		mBt3.setSelected(switchBtValue[2]);
	}

	private void findView() {
		system_back = findViewById(R.id.btn_left);
		unLogin = (Button) findViewById(R.id.system_unLogin);
		title = (TextView) findViewById(R.id.tv_title);
		title.setText("系统设置");

		mBt1 = (ImageView) findViewById(R.id.switch_tixing);
		mBt2 = (ImageView) findViewById(R.id.switch_yuye);
		mBt3 = (ImageView) findViewById(R.id.switch_zhuan);
	}

	private void initSetting() {
		// if (AppConfigFactory.getInstance().getUserInfo() == null) {
		// return;
		// }
		new SimpleBaseCommand() {

			@Override
			public Result go() {
				return new HttpTemplate().exchange(ConstantValue.COMMON
						+ "expert/setting/find/uid?userid="
						+ AppConfigFactory.getInstance().getUserInfo()
								.getUserid());
			}

			@Override
			public void yes(Result result) {
				try {
					JSONObject jo = new JSONObject(result.object.toString())
							.getJSONObject("body");
					settingId = jo.getLong("id");
					switchBtValue[0] = jo.getInt("patientsRemind") == 1;
					switchBtValue[1] = jo.getInt("doctorRemind") == 1;
					switchBtValue[2] = jo.getInt("orderactivityRemind") == 1;
					reset();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void no(Result result) {
			}

		}.execute();
	}

}
